const express = require('express');
const jwt = require('express-jwt');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const setting = require('./config/setting.js');
const userRouter = require('./router/user.js');
const app = express();
const port = 8888;

// 允许所有的请求形式
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept'
  );
  next();
});

// 解析body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// 解析cookie
app.use(cookieParser());
// jwt
app.use(
  jwt({
    secret: setting.token.secret,
    algorithms: setting.token.algorithms,
  }).unless({
    path: setting.token.path,
  })
);

app.use('/user', userRouter);

app.listen(port, () => {
  console.log(`服务已启动，端口：${port}。`);
});
